package com.liugan.magic.actor;

import org.loon.framework.android.game.core.geom.RectBox;

import com.liugan.magic.actor.base.BaseActor;
import com.liugan.magic.screen.PointXY;
import com.liugan.magic.screen.layer.GameLayer;
import com.liugan.magic.utils.AnimationUtils;

public class Rabbit extends BaseActor{
	
	//为自动走路添加的
	double initx,inity;
	
	//运动类型,运动区域
	private int standsecond;
	private int runarea;
	private String runtype="";
	public void setRunarea(int runarea) {
		this.runarea = runarea;
	}
	public void setruntype(String runtype){
		this.runtype=runtype;
	}
	
	public Rabbit(PointXY totalXY,PointXY offsetXY){
		this.totalXY.setxy(totalXY);
		this.offsetXY.setxy(offsetXY);
		this.setAnimation(true);
		this.setAnimation(AnimationUtils.getinstance().rrsa);
		ct.setCT("rabbit",0,0,1,4,-15,0,0);//设定属性
		//
		isdir="right";
		//后来添加的
		initx=totalXY.getX();
		inity=totalXY.getY();
		vx=ct.xs;
		vy=0;
		runarea=100;
		runtype="area";
	}

	@Override
	public void action(long elapsedTime) {
		if (layer==null){layer =(GameLayer)this.getLayer();}
		this.setLocation(this.totalXY.x-layer.layerbeginXY.x, this.totalXY.y-layer.layerbeginXY.y);
		
		if ("area".equals(runtype)){
			runinarea();
		}else if("animation".equals(runtype)){
			runinAn();
		}else if("".equals(runtype)){
			runinarray();
		}
		
	}
	
	
	/** 范围运动
	 * 
	 */
	public void runinarea(){
		//休息几个回合
		standsecond+=1;
		if (standsecond<=120){
			if (standsecond<40){
				if ("right".equals(isdir)){
					this.setAnimation(true);
					this.setAnimation(AnimationUtils.getinstance().rlsa);
				}else{
					this.setAnimation(true);
					this.setAnimation(AnimationUtils.getinstance().rrsa);
				}
			}else{
				if ("right".equals(isdir)){
					this.setAnimation(true);
					this.setAnimation(AnimationUtils.getinstance().rrsa);
				}else{
					this.setAnimation(true);
					this.setAnimation(AnimationUtils.getinstance().rlsa);
				}
			}
		}else{
			//走动
			totalXY.x+=vx;
			//根据速度改变面向方向
				if (vx>0){
					this.setAnimation(true);
					this.setAnimation(AnimationUtils.getinstance().rrra);
					isdir="right";
				}else{
					this.setAnimation(true);
					this.setAnimation(AnimationUtils.getinstance().rlra);
					isdir="left";
				}
			//范围
			if(Math.abs(initx-totalXY.x)>runarea){
				vx=-vx;
				//该休息了
				standsecond=1;
				if (vx>0){
					
					isdir="right";
				}else{
					isdir="left";
				}
			}
		}

		if (!onGround){
			vy += ct.slowys;//减速
			newy=totalXY.y+(int)vy;
		}
		if (vy>0&&!onGround){
			if (vy>0){
				for (int i=0;i<layer.getActorList().size();i++){
					BaseActor ba=layer.getActorList().get(i);
					RectBox newrbox=new RectBox(totalXY.x+ct.rx,newy+ct.ry,getWidth()-ct.rwidth,getHeight()-ct.rheight);
					RectBox actorbox=new RectBox(ba.totalXY.x+ct.rx,ba.totalXY.y+ct.ry,ba.getWidth()-ba.ct.rwidth,ba.getHeight()-ba.ct.rheight);
					if(newrbox.intersects(actorbox)){
						if ((this.getY()+getHeight())<(ba.getY()+ba.getHeight())){//脚碰地
							float temy=(ba.getY()+ba.ct.ry)-(getY()+getHeight());
							totalXY.y+=temy;
							vy = 0;
							onGround=true;
							break;
						}
					}
					if (i==(layer.getActorList().size()-1)){
						totalXY.y+=vy;
					}
				}
			}
		}
	}
	
	/** 在单个动画下运行
	 * 
	 */
	public void runinAn(){
		
	}
	
	/** 使用坐标数组运行
	 * 
	 */
	public void runinarray(){
		
	}
	
	
}
